﻿using UnityEngine;
using System.Collections;
using System.Collections.Generic;

//背包管理
class BagMgr
{
    //打开背包面板
    public void OpenBagPanel()
    {

    }

    public int BagSize
    {
        get
        {
            return 24;
        }
    }

    //==================================================== Message
    //道具唯一ID集合
    private static Dictionary<int, ItemInfo> ItemInfoHash;
    //道具在背包格子集合
    private static Dictionary<int, ItemInfo> ItemGridIdHash;

    public Dictionary<int, ItemInfo>.ValueCollection itemInfos
    {
        get
        {
            return ItemInfoHash.Values;
        }
    }

    //收到背包道具总列表 上线发一次
    public void ResBagTotalItemList(List<ItemInfo> itemList)
    {
        if(ItemInfoHash == null)
            ItemInfoHash = new Dictionary<int, ItemInfo>();
        if (ItemGridIdHash == null)
            ItemGridIdHash = new Dictionary<int, ItemInfo>();

        foreach (ItemInfo info in itemList)
        {
            ItemInfoHash.Add(info.ItemId, info);
            ItemGridIdHash.Add(info.GridId, info);
        }
    }

    //收到道具增加
    public void ResBagAddItem(ItemInfo item)
    {
        ItemInfoHash.Add(item.ItemId, item);
        ItemGridIdHash.Add(item.GridId, item);
        bagEvent.DispatchEvent(BagEvent.BAGITEMADD, item);
    }

    //收到道具删除
    public void ResBagDelItem(ItemInfo item)
    {
        ItemInfoHash.Remove(item.ItemId);
        ItemGridIdHash.Remove(item.GridId);
        bagEvent.DispatchEvent(BagEvent.BAGITEMDEL, item);
    }

    //收到道具改变
    public void ResBagChangeItem(ItemInfo item)
    {
        ItemInfoHash[item.ItemId] = item;
        ItemGridIdHash[item.GridId] = item;
        bagEvent.DispatchEvent(BagEvent.BAGITEMCHANGE, item);
    }

    //收到道具移动结果
    public void ResMoveItem(ItemInfo item, int GridId)
    {
        if(item != null)
        {
            ItemGridIdHash[item.GridId] = item;
        }
        else
        {
            ItemGridIdHash.Remove(GridId);
        }

        bagEvent.DispatchEvent(BagEvent.BagItemMove, GridId);
        
    }

    //收到道具使用结果
    public void ResUseItem(int itemId, int useNum, bool UseSuccess = true)
    {
        Debug.Log("道具ID：" + itemId + "使用成功");
    }

    //请求使用道具
    public void ReqUseItem(ItemInfo  item, int useNum)
    {
        ItemInfo useItem = item.Clone();
        useItem.ItemNum = useNum;
        Mgr.bagServerMgr.ResUseItem(useItem);
    }

    //请求移动道具到其他格子
    public void ReqMoveItem(ItemInfo item, int toGridId)
    {
        Mgr.bagServerMgr.ResMoveItem(item, toGridId);

    }

    //获取格子上的道具
    public ItemInfo GetItemByGridIdx(int idx)
    {
        if (ItemGridIdHash.ContainsKey(idx))
            return ItemGridIdHash[idx];
        else
            return null;

    }


    //事件
    public BagEvent bagEvent = new BagEvent();

    //==============================================
    private static BagMgr _instance;
    public static BagMgr Instance
    {
        private set { }
        get
        {
            if (_instance == null)
                _instance = new BagMgr();
            return _instance;
        }
    }

    

}
